package com.ruoyi.supplier.service.impl;

import java.util.List;

import com.ruoyi.common.constant.BusinessConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.NumberUtils;
import com.ruoyi.supplier.domain.TbPurchaseDTO;
import com.ruoyi.supplier.domain.TbStoreIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.supplier.domain.TbPurchaseDetail;
import com.ruoyi.supplier.mapper.TbPurchaseMapper;
import com.ruoyi.supplier.domain.TbPurchase;
import com.ruoyi.supplier.service.ITbPurchaseService;

/**
 * 采购Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-16
 */
@Service
public class TbPurchaseServiceImpl implements ITbPurchaseService {
    @Autowired
    private TbPurchaseMapper tbPurchaseMapper;

    /**
     * 查询采购
     *
     * @param id 采购ID
     * @return 采购
     */
    @Override
    public TbPurchase selectTbPurchaseById(Long id) {
        return tbPurchaseMapper.selectTbPurchaseById(id);
    }

    /**
     * 查询采购列表
     *
     * @param tbPurchase 采购
     * @return 采购
     */
    @Override
    public List<TbPurchase> selectTbPurchaseList(TbPurchase tbPurchase) {
        return tbPurchaseMapper.selectTbPurchaseList(tbPurchase);
    }

    /**
     * 查询采购明细
     *
     * @param tbPurchaseDTO
     * @return
     */
    @Override
    public List<TbPurchaseDTO> selectTbPurchaseDetailList(TbPurchaseDTO tbPurchaseDTO) {
        return tbPurchaseMapper.selectTbPurchaseDetailList(tbPurchaseDTO);
    }

    /**
     * 查询当前最大采购批次号
     *
     * @return
     */
    @Override
    public String selectNumber() {
        String number = tbPurchaseMapper.selectNumber();
        return NumberUtils.autoGenericNumber(number,BusinessConstants.CG);
    }


    /**
     * 新增采购
     *
     * @param tbPurchase 采购
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbPurchase(TbPurchase tbPurchase) {
        tbPurchase.setCreateTime(DateUtils.getNowDate());
        tbPurchase.setCreateBy(SecurityUtils.getUsername());
        int rows = tbPurchaseMapper.insertTbPurchase(tbPurchase);
        insertTbPurchaseDetail(tbPurchase);
        return rows;
    }

    /**
     * 修改采购
     *
     * @param tbPurchase 采购
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbPurchase(TbPurchase tbPurchase) {
        tbPurchase.setUpdateTime(DateUtils.getNowDate());
        tbPurchase.setUpdateBy(SecurityUtils.getUsername());
        tbPurchaseMapper.deleteTbPurchaseDetailByPurchaseId(tbPurchase.getId());
        insertTbPurchaseDetail(tbPurchase);
        return tbPurchaseMapper.updateTbPurchase(tbPurchase);
    }

    /**
     * 批量删除采购
     *
     * @param ids 需要删除的采购ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbPurchaseByIds(Long[] ids) {
        for (Long id : ids) {
            TbPurchase tbPurchase = tbPurchaseMapper.selectTbPurchaseById(id);
            if (tbPurchase.getStatus() > 0) {

                return -1;
            }
        }
        tbPurchaseMapper.deleteTbPurchaseDetailByPurchaseIds(ids);
        return tbPurchaseMapper.deleteTbPurchaseByIds(ids);
    }

    /**
     * 删除采购信息
     *
     * @param id 采购ID
     * @return 结果
     */
    @Override
    public int deleteTbPurchaseById(Long id) {
        tbPurchaseMapper.deleteTbPurchaseDetailByPurchaseId(id);
        return tbPurchaseMapper.deleteTbPurchaseById(id);
    }


    /**
     * 新增采购明细信息
     *
     * @param tbPurchase 采购对象
     */
    public void insertTbPurchaseDetail(TbPurchase tbPurchase) {
        List<TbPurchaseDetail> tbPurchaseDetailList = tbPurchase.getTbPurchaseDetailList();
        Long id = tbPurchase.getId();
        if (StringUtils.isNotNull(tbPurchaseDetailList)) {
            List<TbPurchaseDetail> list = new ArrayList<TbPurchaseDetail>();
            for (TbPurchaseDetail tbPurchaseDetail : tbPurchaseDetailList) {
                tbPurchaseDetail.setPurchaseId(id);
                list.add(tbPurchaseDetail);
            }
            if (list.size() > 0) {
                tbPurchaseMapper.batchTbPurchaseDetail(list);
            }
        }
    }
}
